{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b96149ab-85d5-4112-9f7c-44a486dd340f",
   "metadata": {},
   "source": [
    "Runtime: 32 ms, faster than 68.26% of Python3 online submissions for Unique Binary Search Trees.\n",
    "Memory Usage: 14.4 MB, less than 15.92% of Python3 online submissions for Unique Binary Search Trees."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bbb570e-7527-4664-89c6-4e179c370212",
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import lru_cache\n",
    "\n",
    "class Solution:\n",
    "    def numTrees(self, n: int) -> int:\n",
    "        @lru_cache(maxsize=None)\n",
    "        def countPossibleWays(numberOfNodes: int):\n",
    "            if (numberOfNodes <= 1):\n",
    "                return 1\n",
    "            \n",
    "            result = 0\n",
    "            for how_many_nodes_in_left in range(numberOfNodes):\n",
    "                left_possible_combinations = countPossibleWays(how_many_nodes_in_left)\n",
    "\n",
    "                how_many_nodes_in_right = numberOfNodes - 1 - how_many_nodes_in_left\n",
    "\n",
    "                right_possible_combinations = countPossibleWays(how_many_nodes_in_right)\n",
    "\n",
    "                result += left_possible_combinations * right_possible_combinations\n",
    "                \n",
    "            return result\n",
    "        return countPossibleWays(n)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
